<template>
  <el-container :v-loading="loading" class="post-article">
    <el-header class="header">
      <el-input
        v-model="comarticle.title"
        placeholder="请输入标题..."
        style="width: 400px; margin-top: 5px"
      ></el-input>
    </el-header>
    <el-main class="main">
      <div id="editor">
        <mavon-editor
          style="height: 100%; width: 100%"
          ref="md"
          @imgAdd="imgAdd"
          @imgDel="imgDel"
          v-model="comarticle.mdcontent"
        ></mavon-editor>
      </div>
      <div
        style="
          display: flex;
          align-items: center;
          margin-top: 15px;
          justify-content: flex-end;
        "
      >
        <template>
          <el-button type="primary" @click="saveEdit()">保存修改</el-button>
        </template>
        <template>
          <el-button type="primary" @click="giveup()">放弃修改</el-button>
        </template>
      </div>
    </el-main>
  </el-container>
</template>

<script>
import { isNotNullORBlank } from "@/utils/utils";
import {
  updatecom,
  updatepeo,
  updateres,
  updatebri,
  comid,
  resu,
  parpeo,
} from "@/api/apis";
export default {
  name: "Editor",
  data() {
    return {
      loading: true,
      comarticle: {
        title: "",
        content: "",
        mdcontent: "",
        model: "",
      },
    };
  },
  methods: {
    giveup() {
      this.$router.go(-1);
    },
    saveEdit() {
         this.comarticle.title = this.comarticle.title,
         this.comarticle.content = this.$refs.md.d_render,
         this.comarticle.mdcontent = this.comarticle.mdcontent,
         this.comarticle.model = this.comarticle.model;
      if (this.comarticle.model == 3) {
        updatecom(this.comarticle).then((res) => {
          //  console.log(res)
          if (res.data.code == 200) {
            this.$message({
              message: "恭喜你，修改成功",
              type: "success",
            });
            this.$router.go(-1);
          } else {
            this.$message.error("请稍后再试~~~~");
          }
        });
      }
      if (this.comarticle.model == 1 || this.comarticle.model == 2) {
        updatepeo(this.comarticle).then((res) => {
          //  console.log(res)
          if (res.data.code == 200) {
            this.$message({
              message: "恭喜你，修改成功",
              type: "success",
            });
            this.$router.go(-1);
          } else {
            this.$message.error("请稍后再试~~~~");
          }
        });
      }
      if (this.comarticle.model == 4) {
        updateres(this.comarticle).then((res) => {
          //  console.log(res)
          if (res.data.code == 200) {
            this.$message({
              message: "恭喜你，修改成功",
              type: "success",
            });
            this.$router.go(-1);
          } else {
            this.$message.error("请稍后再试~~~~");
          }
        });
      }
      if (this.comarticle.model == 5) {
        updatebri(this.comarticle).then((res) => {
          //  console.log(res)
          if (res.data.code == 200) {
            this.$message({
              message: "恭喜你，修改成功",
              type: "success",
            });
            this.$router.go(-1);
          } else {
            this.$message.error("请稍后再试~~~~");
          }
        });
      }
    },
  },
  mounted() {
    this.loading = true;
    // console.log(this.$route.query)
    this.comarticle.title = this.$route.query.row.title;
    // this.comarticle.content=this.$route.query.row.content
    // this.comarticle.mdcontent=this.$route.query.row.mdcontent
    this.comarticle.model = this.$route.query.row.model;
    this.comarticle.id = this.$route.query.row.id;
    if (this.comarticle.model == 3) {
      comid(this.comarticle.id).then((res) => {
        // console.log(res);
        this.comarticle.mdcontent = res.data.data[0].mdcontent;
      });
    }
    //研究生
    // 本科生
    if (this.comarticle.model == 1 || this.comarticle.model == 2) {
      parpeo(this.comarticle.id).then((res) => {
        // console.log(res);
        this.comarticle.mdcontent = res.data.data[0].mdcontent;
      });
    }
    if (this.comarticle.model == 4) {
      resu(this.comarticle.id).then((res) => {
        // console.log(res);
        this.comarticle.mdcontent = res.data.data[0].mdcontent;
      });
    }
    if (this.comarticle.model == 5) {
      this.comarticle.mdcontent = this.$route.query.row.mdcontent;
    }

    // console.log(typeof this.$route.query.row.model)
  },
};
</script>
<style>
.post-article > .header {
  background-color: #ececec;
  margin-top: 10px;
  padding-left: 5px;
  display: flex;
  justify-content: flex-start;
}
.post-article > .main > #editor {
  width: 100%;
  height: 450px;
  text-align: left;
}

.post-article > .main {
  /*justify-content: flex-start;*/
  display: flex;
  flex-direction: column;
  padding-left: 5px;
  background-color: #ececec;
  padding-top: 0px;
}
</style>

